package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    JedisPool jedisPool;
    @Reference
    MemberService memberService;


    @RequestMapping("check.do")
    public Result check(@RequestBody Map map){
        String telephone = map.get("telephone").toString() ;
        Member member= memberService.getMember(telephone);
        if (member == null){
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.add(member);
        }
        String validateCode = map.get("validateCode").toString();
        String redisCode = jedisPool.getResource().get(telephone+ RedisMessageConstant.SENDTYPE_LOGIN);
        if (redisCode == null || !validateCode.equals(redisCode)){
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }else {
            return  new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }
    }
}
